package com.imooc.controller;

import com.imooc.enums.YesOrNo;
import com.imooc.pojo.Carousel;
import com.imooc.pojo.Category;
import com.imooc.pojo.VO.CategoryVO;
import com.imooc.pojo.VO.NewItemVO;
import com.imooc.service.CarouselService;
import com.imooc.service.CategoryService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lihongyu
 * @CreateTime 2021-08-10 13:53
 * @Description 首页控制器
 */
@Api(value = "首页",tags = {"首页相关接口"})
@RestController
@RequestMapping("/index")
public class IndexController {

    @Resource
    private CarouselService carouselService;

    @Resource
    private CategoryService categoryService;

    /**
     * 获取轮播图列表
     * @return 轮播图列表
     */
    @ApiOperation(value = "获取轮播图列表", notes = "获取轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public IMOOCJSONResult getCarouselList(){
        List<Carousel> carouselList = carouselService.queryAllCarouselList(YesOrNo.YES.type);
        return IMOOCJSONResult.ok(carouselList);
    }

    /**
     * 获取一级分类列表
     * @return 一级分类信息
     */
    @ApiOperation(value = "获取一级分类列表", notes = "获取一级分类列表", httpMethod = "GET")
    @GetMapping("/cats")
    public IMOOCJSONResult getRootCategoryList(){
        List<Category> carouselList = categoryService.getRootCategoryList();
        return IMOOCJSONResult.ok(carouselList);
    }

    /**
     * 根据一级分类编码获取所有子分类列表
     * @return 子分类列表
     */
    @ApiOperation(value = "根据一级分类编码获取所有子分类列表", notes = "根据一级分类编码获取所有子分类列表", httpMethod = "GET")
    @GetMapping("/subCat/{categoryId:\\d+}")
    public IMOOCJSONResult getSubCategoryListByRootCategoryId(@PathVariable Integer categoryId){
        if(categoryId == null || categoryId <= 0){
            return IMOOCJSONResult.errorMsg("一级分类编码为空!");
        }
        List<CategoryVO> categoryVOList = categoryService.getSubCategoryListByRootCategoryId(categoryId);
        return IMOOCJSONResult.ok(categoryVOList);
    }

    /**
     * 根据一级分类编码获取分类下六个最新商品信息
     * @param rootCatId 一级分类编码
     * @return 分类下六个最新商品
     */
    @GetMapping("/sixNewItems/{rootCatId}")
    @ApiOperation(value = "获取分类下六个最新商品信息", notes = "获取分类下六个最新商品信息", httpMethod = "GET")
    public IMOOCJSONResult getSixNewItems(@PathVariable Integer rootCatId){
        List<NewItemVO> newItemVOList = categoryService.getSixNewItemsByRootCatId(rootCatId);
        return IMOOCJSONResult.ok(newItemVOList);
    }


}
